package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NCement1AlarmBo;
import com.ruoyi.zhgd.domain.vo.NCement1AlarmVo;
import com.ruoyi.zhgd.domain.NCement1Alarm;
import com.ruoyi.zhgd.mapper.NCement1AlarmMapper;
import com.ruoyi.zhgd.service.INCement1AlarmService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.baomidou.dynamic.datasource.annotation.DS;

/**
 * 沥青拌合站筛孔数据Service业务层处理
 *
 * @author zyb
 * @date 2023-09-06
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NCement1AlarmServiceImpl implements INCement1AlarmService {

    private final NCement1AlarmMapper baseMapper;

    /**
     * 查询沥青拌合站筛孔数据
     */
    @Override
    public NCement1AlarmVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public TableDataInfo<NCement1AlarmVo> queryPageList(NCement1AlarmBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NCement1Alarm> lqw = buildQueryWrapper(bo);
        Page<NCement1AlarmVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青拌合站筛孔数据列表
     */
    @Override
    public List<NCement1AlarmVo> queryList(NCement1AlarmBo bo) {
        LambdaQueryWrapper<NCement1Alarm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NCement1Alarm> buildQueryWrapper(NCement1AlarmBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NCement1Alarm> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGravel1() != null, NCement1Alarm::getGravel1, bo.getGravel1());
        lqw.eq(bo.getGravel2() != null, NCement1Alarm::getGravel2, bo.getGravel2());
        lqw.eq(bo.getGravel3() != null, NCement1Alarm::getGravel3, bo.getGravel3());
        lqw.eq(bo.getGravel4() != null, NCement1Alarm::getGravel4, bo.getGravel4());
        lqw.eq(bo.getGravel5() != null, NCement1Alarm::getGravel5, bo.getGravel5());
        lqw.eq(bo.getGravel6() != null, NCement1Alarm::getGravel6, bo.getGravel6());
        lqw.eq(bo.getPowder1() != null, NCement1Alarm::getPowder1, bo.getPowder1());
        lqw.eq(bo.getPowder2() != null, NCement1Alarm::getPowder2, bo.getPowder2());
        lqw.eq(bo.getWater() != null, NCement1Alarm::getWater, bo.getWater());
        lqw.eq(bo.getAlarm() != null, NCement1Alarm::getAlarm, bo.getAlarm());
        lqw.eq(StringUtils.isNotBlank(bo.getDetails()), NCement1Alarm::getDetails, bo.getDetails());
        lqw.eq(bo.getState() != null, NCement1Alarm::getState, bo.getState());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmReason()), NCement1Alarm::getAlarmReason, bo.getAlarmReason());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPlan()), NCement1Alarm::getHandlerPlan, bo.getHandlerPlan());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerResult()), NCement1Alarm::getHandlerResult, bo.getHandlerResult());
        lqw.eq(bo.getHandlerTime() != null, NCement1Alarm::getHandlerTime, bo.getHandlerTime());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerPerson()), NCement1Alarm::getHandlerPerson, bo.getHandlerPerson());
        return lqw;
    }

    /**
     * 新增沥青拌合站筛孔数据
     */
    @Override
    public Boolean insertByBo(NCement1AlarmBo bo) {
        NCement1Alarm add = BeanUtil.toBean(bo, NCement1Alarm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改沥青拌合站筛孔数据
     */
    @Override
    public Boolean updateByBo(NCement1AlarmBo bo) {
        NCement1Alarm update = BeanUtil.toBean(bo, NCement1Alarm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NCement1Alarm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青拌合站筛孔数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
